﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Zhongchouwwwww.Models;
using Zhongchouwwwww.BAL;
using System.Text.RegularExpressions;

namespace Zhongchouwwwww.webform
{
    public partial class user_register : System.Web.UI.Page
    {
        private static ZhongchouDBEntities db = BAL.GetDate.db;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }
        }

        public bool Check()
        {
            if (user_pwd.Text != user_repwd.Text)
            {
                Response.Write(@"<script language >alert('两次密码输入不一致！')<" + "/script>");
                return false;
            }

            if (!CheckBox.Checked)
            {
                Response.Write(@"<script language >alert('请查看《账号协议》并勾选！')<" + "/script>");
                return false;
            }
            try
            {
                if (db.Database.SqlQuery<string>($"select usernikename from Userinfo where usernikename = '{user_name.Text}'").First() == user_name.Text)
                {
                    Response.Write(@"<script language >alert('用户昵称已存在，换一个试试吧！')<" + "/script>");
                    return false;
                }
            }
            catch (Exception)
            {
                return true;
            }
            return true;
        }
       
        protected void btn_register_Click(object sender, EventArgs e)
        {
            if (!Check())
                return;
            if (!BAL.DataVerification.IsHandset(user_nums.Text)) {
                Response.Write(@"<script language >alert('输入的电话号码不合法！')<" + "/script>");
                return;
            }
            if (YZM.ImageUrl.Substring(13, 4) != user_yzm.Text)
            {
                Response.Write(@"<script language >alert('验证码错误！')<" + "/script>");
                return;
            }
            Userinfo userer = new Userinfo
            {
                userphone = user_nums.Text,
                usernikename = user_name.Text,
                userpwd = user_pwd.Text,
                sex = "保密",
                registertime = DateTime.Now,
                userstate = "正常",
                nikenametime=DateTime.Now.AddDays(-35),
                imgurl= "../images/DefautIcon.jpg",
                userdesc="写下你的个人说明吧",
                address= "广西南宁"
            };
            db.Userinfo.Add(userer);
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("更新条目时出错"))
                {
                    Response.Write(@"<script language>alert('手机号已注册！')</script>");
                    return;
                }
            }
            finally
            {
                Response.Redirect("user-login.aspx?id=true");
            }
        }

        protected void YZM_Click(object sender, ImageClickEventArgs e)
        {
            int num = new Random().Next(1, 4);
            switch (num)
            {
                case 1:
                    YZM.ImageUrl = "../images/yzm7364.jpg";
                    break;
                case 2:
                    YZM.ImageUrl = "../images/yzmvwo7.jpg";
                    break;
                case 3:
                    YZM.ImageUrl = "../images/yzmzvyk.jpg";
                    break;
                default:
                    break;
            }
        }
    }
}